 
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>组合成分</title>
	<link rel="stylesheet" type="text/css" href="../../rs/plugins/easyui/themes/metro/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../rs/plugins/easyui/themes/icon.css">
	<link href="../../rs/css/bootstrap.min.css" rel="stylesheet">
	<script type="text/javascript" src="../../rs/plugins/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../../rs/plugins/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../rs/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>
	<script src="../../rs/js/jquery.cookie.js"></script>
	<script src="../../rs/js/common_qj.js"></script>
	<style>
		html,body{ margin:0px; height:100%;}
		.ms-container {
			background: transparent url(../../rs/img/switch.png) no-repeat 50% 50% !important;
			padding: 10px;
		}
		.ms-container:after {
			content: ".";
			display: block;
			height: 0;
			line-height: 0;
			font-size: 0;
			clear: both;
			min-height: 0;
			visibility: hidden;
		}
		.ms-container .ms-selectable, .ms-container .ms-selection {
			background: #fff;
			color: #555555;
			float: left;
			width: 48%;
		}
		.ms-container .ms-selection {
			float: right;
		}

		.ms-container .ms-list {
			-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
			-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
			box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
			-webkit-transition: border linear 0.2s, box-shadow linear 0.2s;
			-moz-transition: border linear 0.2s, box-shadow linear 0.2s;
			-ms-transition: border linear 0.2s, box-shadow linear 0.2s;
			-o-transition: border linear 0.2s, box-shadow linear 0.2s;
			transition: border linear 0.2s, box-shadow linear 0.2s;
			-webkit-border-radius: 3px;
			-moz-border-radius: 3px;
			border-radius: 3px;
			position: relative;
			min-height: 450px !important;
			padding: 0;
			overflow-y: auto;}
	</style>
	<?php
  		require_once('login.php');
 	?>
</head>
<body >
<table id="table" class="easyui-datagrid"  style="width:100%;height:100%;padding: 10px;"
	   data-options="
			rownumbers:true,
			singleSelect:'false',
			method:'get' ,
			toolbar:'#easyui_toolbar',
  			fit:true,
			fitColumns:true">
	<thead>
	<tr>
		<th data-options="field:'prid',align:'center',width:10,formatter:formatId">编号</th>
		<th data-options="field:'prname1',width:30">名称</th>
		<th data-options="field:'prpricein',width:30">价格</th>
		<th data-options="field:'prsort',width:30" >分类</th>
		<th data-options="field:'sl',width:30,formatter:formatcf" >组合成分数</th>
	</tr>
	</thead>
</table>

<div id="easyui_toolbar" region="north" border="false"
	 style="height: 45px;padding: 5px 5px; ">

	<div id="tb" class="input-group" style="float: right; margin-right: 10px;width:350px">
		<input type="text" class="form-control" id="searchtext" placeholder="输入搜索关键字" value="">
		<div class="input-group-btn">
			<button type="button" class="btn btn-white dropdown-toggle" onclick="do_search()" style="border-radius: 0px;">
				搜索
			</button>
		</div>
	</div>
</div>


<div id="easyui_toolbar_l" region="center" border="false"
	 style="height: 45px;padding: 5px 5px; ">
	<div class="row">
		<div class="col-sm-6" style="padding-right:5px;">
			<div class="input-group ">
				<span class="input-group-btn">
					<button style="margin-right: -2px;height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">类别</button>
				</span>
				<select class="form-control" id="get_lb" onchange="show_sub()">
					<option value=''>全部分类</option>
				</select>
			</div>
		</div>

		<div class="col-sm-6" style="padding-left:0px;">
			<div class="input-group ">
				<input type="text" class="form-control" id="searchtext_l" placeholder="输入搜索关键字">
				<div class="input-group-btn">
					<button type="button" class="btn btn-white dropdown-toggle" onclick="do_search_l()" style="border-radius: 0px;">
						搜索
					</button>
				</div>
			</div>
		</div>

	</div>
</div>

<div id="easyui_toolbar_r" region="center" border="false"
	 style="height: 45px;padding: 5px 5px; ">
	<div class="input-group " style="    width: 100%;">
		<button style="float:left; margin-right:5px; border-radius: 0px;height:34px "  class="btn" onclick="data_remove_r()" >
			<i class="glyphicon glyphicon-trash"></i>
		</button>

		<button style="float:left;margin-left:5px;  border-radius: 0px;" class="btn " onclick="queren()"  >
			确认
		</button>
		<button type="button" class="btn " data-toggle="dropdown"
				style="  float: right;margin-right:5px;  border-radius: 0px;" id="save_do">保存
		</button>
	</div>
</div>


<div style="display: none" id="open_edit">
	<div id="edit_from">
		<div class="ms-container" id="ms-callbacks">
			<div class="ms-selectable">
				<div style="text-align: center;padding-bottom: 5px;font-weight: 700;">未选择</div>
				<div class="ms-list" >

					<table id="table_l" class="easyui-datagrid"  style="height:500px"
						   data-options="
							rownumbers:true,
							singleSelect:'false',
							toolbar:'#easyui_toolbar_l',
							method:'get' ,
							height: 250,
							fit:false,
							fitColumns:true">
						<thead>
						<tr>
							<th data-options="field:'prid',width:15">编号</th>
							<th data-options="field:'prname1',width:50">名称</th>


						</tr>
						</thead>
					</table>

				</div>
			</div>
			<div class="ms-selection">
				<div style="text-align: center;padding-bottom: 5px;font-weight: 700;">已选择</div>
				<div class="ms-list" >
					<table id="table_r" class="easyui-datagrid"  style=" "
						   data-options="
							rownumbers:true,
							singleSelect:'false',
							toolbar:'#easyui_toolbar_r',
							method:'get' ,
							fit:false,
							fitColumns:true,
							onClickCell: onClickCell,
							showFooter :true,
							onEndEdit: onEndEdit
							">
						<thead>
						<tr>
							<th data-options="field:'ck',checkbox:true"></th>
							<th data-options="field:'prname1',width:30"  >品名</th>
							<th data-options="field:'prunit',width:15"  >单位</th>
							<th data-options="field:'sl',width:15,editor:'text',align:'right'" >数量</th>
							<th data-options="field:'prpricein',width:15,align:'right' "  >单价</th>
							<th data-options="field:'je',width:15 ">成本</th>
						</tr>
						</thead>
					</table>

				</div>
			</div>
		</div>
	</div>
</div>

<!--页面加载缓冲图标-->
<div id='loadingDiv' style="position:absolute;z-index:1000;top:0px; left:0px;
	width:100%;height:100%;background:white;text-align: center;">
	<h1 style="top: 30%; position: relative;">
		<img src="../../rs/img/loading-2.gif" /><br>
	</h1>
</div>
<script>

	/*页面加载缓冲*/
	function closeLoading() {
		$("#loadingDiv").fadeOut("normal", function () {
			$(this).remove();
		});
	}
	var no;
	$.parser.onComplete = function () {
		if (no) clearTimeout(no);
		no = setTimeout(closeLoading, 1000);
	}

	var sign = URLencode($.cookie('sign'));

	$(function(){
		if(sign==="undefined"){
			window.location.href = "login.html";
		}
		get_lb();
		f_sheard();
		var getdata;
	});

	//判断用户权限&加载组合成分数据
	function f_sheard(){
		$.ajax({
			type: "post",
			url:G_url+'slpr/see2?sign='+sign,
			data: 'sign=' + sign,
			dataType: "json",
			success: function (usedata){
				getdata=usedata;
				if (usedata.ok) {
					$('#table').datagrid({
						data: usedata.data
					});
				}else{
					window.location.href = 'login.html';
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}
	function f_sheard_l(){
		$('#table_l').datagrid({
			data:getdata.data
		});
	}


	var table_r_alldata;
	var	total;
	function f_sheard_r(){
		$('#table_r').datagrid({
			data: []
		});
		$.ajax({
			type: "post",
			url:G_url+'przh/see?sign='+sign,
			data: 'prid=' + getprid,
			dataType: "json",
			success: function (usedata){
				table_r_alldata=usedata.data;
				total=table_r_alldata.footer[0].je;
				if (usedata.ok) {
					$('#table_r').datagrid({
						data: usedata.data
					});

				}else{
					layer.msg(usedata.msg);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}
	$("#save_do").on("click",function(){
		var senddata = $('#table_r').datagrid('getData');
		var data=senddata.rows;
		endEditing();


		if(data.length!=0){
			$.ajax({
				type: "post",
				url:G_url+'przh/save?sign='+sign,
				data:JSON.stringify(data),
				dataType: "json",
				success: function (result){
					if (result.ok){

						layer.msg(result.msg);
					} else {
						layer.msg(result.msg);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				}
			});
		}else{
			$.ajax({
				type: "post",
				url:G_url+'przh/save?sign='+sign,
				data:"&prid=" + getprid,
				dataType: "json",
				success: function (result){
					if (result.ok){

						layer.msg(result.msg);
					} else {
						layer.msg(result.msg);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				}
			});
		}

		$('#table').datagrid('updateRow',{
			index: rowindex,
			row: {
				sl:data.length
			}
		});
	});





	/*删除添加的组合成分*/
	function data_remove_r(){
		var rows = $('#table_r').datagrid('getSelections');
		var row = $('#table_r').datagrid('getSelected');
		var rowIndex = $('#table_r').datagrid('getRowIndex', row);
		if(rows.length!=0){
			var aaa=layer.msg('确定删除吗？', {
						time:0,
						btn: ['确定', '取消'] //按钮
						,yes: function do_remove() {
							var je = $.map(rows, function (row) {
								return row.je;
							});
							layer.close(aaa);
							$('#table_r').datagrid('deleteRow', rowIndex);

							//total=parseInt(total)-parseInt(je);
							//table_r_alldata.footer[0].je=total;
							//$('#table_r').datagrid({
							//	data: table_r_alldata
							//});
							f_total();


						}
					}
			);
		}else{
			layer.msg("请先选择！");
		}
	}
	//删除添加的组合成分





	// 产品搜索操作
	$('#searchtext').bind('keypress',function(event){
		if(event.keyCode ==13) {
			do_search();
		}
	});


	$('#searchtext_l').bind('keydown',function(event){
		if(event.keyCode ==13) {
			do_search_l();
		}
	});

	//搜索产品
	function do_search(){
		var gjc=$('#searchtext').val(); //搜索关键词
		if(gjc==""){
			$('#table').datagrid({
				data: getdata.data
			});
		}else{
			var newdata=[];
			for(var i=0;i<getdata.data.length;i++){
				str=transform(getdata.data[i]).join(',');
				row=str.indexOf(gjc);
				if(row>=0){
					newdata.push(getdata.data[i]);
				}
			}
			$('#table').datagrid({
				data: newdata
			});
		}
	}
	var gjc;

	//按类别搜索
	function show_sub(){
		var sub=$('#get_lb').find("option:selected").val();
		var data_fl=[];
		$.each(getdata.data, function(i, val){
			if(val.prsort==sub)
				data_fl.push(val);
		});

		$('#table_l').datagrid({
			data: data_fl
		});

		if(sub==""){
			$('#table_l').datagrid({
				data: getdata.data
			});
		}
	}

	function do_search_l(){
		var gjc_lb=$('#get_lb').find("option:selected").val(); //搜索的分类
		gjc=$('#searchtext_l').val(); //搜索关键词
		if(gjc==""){
			$('#table_l').datagrid({
				data: getdata.data
			});
		}else{
			var newdata_l=[];
			for(var i=0;i<getdata.data.length;i++){
				str=transform(getdata.data[i]).join(',');
				row=str.indexOf(gjc);
				if(row>=0){
					newdata_l.push(getdata.data[i]);
				}
			}
			$('#table_l').datagrid({
				data: newdata_l
			});
		}
	}

	function transform(obj){
		var arr = [];
		for(var item in obj){
			arr.push(obj[item]);
		}
		return arr;
	}




	var rowindex;  //获取选择的行
	var getprid;  //获取所选行的prid
	function open_edit(r){
		var rowdata=$('#table').datagrid('getData');
		var row=rowdata.rows[r];

		getprid=row.prid;
		rowindex=r;
		if (row){
			$("#open_edit").show();
			$('#edit_from').form('load',row);
			layer.open({
				title: "组合成分",
				type: 1,
				
				offset: '0px',//top顶部偏移
				area: ['100%', '100%'], //宽高
				shadeClose: true,
				content: $('#edit_from')
			});
		};
		f_sheard_l();
		f_sheard_r();
	}
	//
	function formatId(value,rowData,rowIndex){
		return "<a href='javascript:void(0)' onclick='open_edit("+rowIndex+");'>"+value+"</a>";
	}
	function formatcf(value,rowData,rowIndex){
		return  value+"个组合成分";
	}

</script>
<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/md5.js"></script>
<script type="text/javascript">


	//获取类别
	function get_lb(){
		$.ajax({
			type: "post",
			url:G_url+'slst/see?'+sign,
			data:'sign='+sign,
			dataType: "json",
			success: function (result){
				var ff = result.data;
				var html = '';
				$.each(ff, function (a, b) {//直接取出json的prolist做each循环.
					html += "<option value='"+b.p_sort+"'>" + b.p_sort_name + "</option>";//拼接字符串.
				});
				$("#get_lb").append(html);
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	}

	//左边商品移入右边组合成分判断商品是否重复添加
	$("#table_l").datagrid({
		onDblClickRow : function(index, row){
			var table_r_data=$('#table_r').datagrid('getData');
			var data_r=table_r_data.rows;
			var data_a=[];
			for (var i=0; i < data_r.length; i++) {
				data_a.push(data_r[i].prid);
			}

			var getrowprid=row.prid;
			var u=data_a.indexOf(getrowprid);//判断商品是否存在
			if(u<0){
				$('#table_r').datagrid('appendRow',{
					prid:row.prid,
					prid2:getprid,
					prname1:row.prname1,
					prunit:row.prunit,
					prpricein:row.prpricein,
					sl:toDecimal3(1),
					je:row.prpricein
				});

				//total=parseInt(total)+parseInt(row.prpricein);
				//table_r_alldata.footer[0].je=total;
				//$('#table_r').datagrid({
				//	data: table_r_alldata
				//});
				f_total();

			}else{
				layer.msg("商品已存在");
			}
		}
	});

	//确认按钮
	function queren(){
		endEditing();
	}



	$.extend($.fn.datagrid.methods, {
		editCell: function(jq,param){
			return jq.each(function(){
				var opts = $(this).datagrid('options');
				var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
				for(var i=0; i<fields.length; i++){
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor1 = col.editor;
					if (fields[i] != param.field){
						col.editor = null;
					}
				}
				$(this).datagrid('beginEdit', param.index);
				var ed = $(this).datagrid('getEditor', param);
				if (ed){
					if ($(ed.target).hasClass('textbox-f')){
						$(ed.target).textbox('textbox').focus();
					} else {
						$(ed.target).focus();
					}

					$(ed.target).bind('keyup', function()
					{
						if (window.event.keyCode == 13)
						{
							$('#table_r').datagrid('endEdit', editIndex);
						}
					});

				}



				for(var i=0; i<fields.length; i++){
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor = col.editor1;
				}
			});
		}
	});

	var editIndex = undefined;


	function onEndEdit(index, row){
		f_je(index);
		f_total();
		return;
		var ed = $(this).datagrid('getEditor', {
			index: index,
			field: 'productid'
		});
		row.productname = $(ed.target).combobox('getText');

	}

	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#table_r').datagrid('validateRow', editIndex)){
			$('#table_r').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickCell(index, field){
		if (editIndex != index){
			if (endEditing()){
				$('#table_r').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				var ed = $('#table_r').datagrid('getEditor', {index:index,field:field});
				if (ed){
					$(ed.target).bind('keyup', function()
					{
						if (window.event.keyCode == 13)
						{
							$('#table_r').datagrid('endEdit', editIndex);
						}
					});
					($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function(){
					$('#table_r').datagrid('selectRow', editIndex);
				},0);
			}
		}
	}


	function f_total(){
		var table_r_data=$('#table_r').datagrid('getData');
		var data_r=table_r_data.rows;
		var total=0;
		for (var i=0; i < data_r.length; i++) {
			total += data_r[i].sl* data_r[i].prpricein;
		}
		table_r_data.footer[0].je=toDecimal2(total);
		$('#table_r').datagrid('reloadFooter',table_r_data.footer);
	}
	function f_je(rowindex){
		var table_r_data=$('#table_r').datagrid('getData');
		var data_r=table_r_data.rows;
		var sl=data_r[rowindex].sl;
		if(sl==""){
			layer.msg("数量不能为空!");
			$('#table_r').datagrid('updateRow',{
				index: rowindex,
				row: {
					je:data_r[rowindex].prpricein,
					sl:toDecimal3(1)
				}
			});
			return false ;
		}
		$('#table_r').datagrid('updateRow',{
			index: rowindex,
			row: {
				je:toDecimal2(sl* data_r[rowindex].prpricein)
			}
		});
	}
	//结果保留两位小数
	function toDecimal2(x) {
		var f = parseFloat(x);
		if (isNaN(f)) {
			return false;
		}
		var f = Math.round(x*100)/100;
		var s = f.toString();
		var rs = s.indexOf('.');
		if (rs < 0) {
			rs = s.length;
			s += '.';
		}
		while (s.length <= rs + 2) {
			s += '0';
		}
		return s;
	}


	//结果保留三位小数
	function toDecimal3(x) {
		var f = parseFloat(x);
		if (isNaN(f)) {
			return false;
		}
		var f = Math.round(x*1000)/1000;
		var s = f.toString();
		var rs = s.indexOf('.');
		if (rs < 0) {
			rs = s.length;
			s += '.';
		}
		while (s.length <= rs + 3) {
			s += '0';
		}
		return s;
	}


</script>
</body>
</html>
